package com.bjsxt.userscenter.springmvc.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.management.relation.Role;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;
import com.bjsxt.userscenter.auth.service.IAuthService;
import com.bjsxt.userscenter.common.controller.BaseController;
import com.bjsxt.userscenter.common.util.ConstatFinalUtil;
import com.bjsxt.userscenter.common.util.PageInfoUtil;
import com.bjsxt.userscenter.users.pojo.AAdmins;
import com.bjsxt.userscenter.users.pojo.ARole;
import com.bjsxt.userscenter.users.service.IUsersService;

/**
 * 登陆后的管理员操作
 * 
 * url规划:凡是带/back的必须是登陆后才能操作
 * 
 * @author WangshSxt
 *
 */

@Controller
@RequestMapping("/back/admins")
public class AdminsBackController extends BaseController
{
	/**
	 * 登陆后的首页
	 * @return
	 */
	@Resource
	private IUsersService usersService;
	@Resource
	private IAuthService authService;
	@RequestMapping("/main")
	public String main()
	{
		ConstatFinalUtil.SYS_LOGGER.info("--main--");
		return "/back/admins/main";
	}
	
	/**
	 * 登陆后的首页
	 * @return
	 */
	@RequestMapping("/leftMenu")
	public String leftMenu()
	{
		ConstatFinalUtil.SYS_LOGGER.info("--leftMenu--");
		return "/back/admins/leftMenu";
	}
	
	/**
	 * 退出
	 * @return
	 */
	@RequestMapping("/logout")
	public String logout(HttpSession session)
	{
		ConstatFinalUtil.SYS_LOGGER.info("--logout--");
		/* 删除session中的信息 */
		session.removeAttribute("admins");
		return "redirect:/login.htm" ; 
	}
	@RequestMapping("/adminsList")
	public String adminsList(String keyword,String status,String st,String ed,String currentPage,String pageSize,Model model)
	{
		ConstatFinalUtil.SYS_LOGGER.info("---adminsList---");
		if(st==null){
			st="";
		}
		if(ed == null)
		{
			ed = "" ; 
		}
		if(status == null)
		{
			status = "" ; 
		}
		Map<String, Object> condMap=new HashMap<String, Object>();
		condMap.put("keyword",keyword );
		if(!"".equalsIgnoreCase(status))
		{
			condMap.put("status", status);
		}
		Date stDate = null ; 
		Date edDate = null ;
		if(!"".equalsIgnoreCase(st) && !"".equalsIgnoreCase(ed))
		{
			/*将字符串转换成日期
			 * 
			 * 完善了DateFormatUtil
			 * */
			stDate = this.dateFormatUtil.strDateTime(st); 
			edDate = this.dateFormatUtil.strDateTime(ed); ; 
		}
		condMap.put("stDate", stDate);
		condMap.put("edDate", edDate);
		
		PageInfoUtil pageInfoUtil = proccedPageInfo(currentPage, pageSize);
		List<AAdmins> adminsList = this.usersService.findCondListAdminsService(pageInfoUtil,condMap);
		/* 将结果放到四大作用域中 */
		model.addAttribute("adminsList", adminsList);
		model.addAttribute("pageInfoUtil", pageInfoUtil);
		
		/* 将查询条件放到request中 */
		model.addAttribute("keyword", keyword);
		model.addAttribute("status", status);
		model.addAttribute("st", st);
		model.addAttribute("ed", ed);
		return "/back/admins/adminsList";
	}
	@RequestMapping("/adminsInsert")
	public String adminsInsert(Model model){
		ConstatFinalUtil.SYS_LOGGER.info("--adminsInsert---");
		Map<String, Object> condMap=new HashMap<String, Object>();
		List<ARole> roleList=usersService.findCondListRoleService(null, condMap);
		model.addAttribute("roleList",roleList);
		
		return "/back/admins/adminsInsert";
	}
	@RequestMapping(value="/adminsInsertSubmit",produces="text/html;charset=utf-8")
	@ResponseBody
	public String adminsInsertSubmit(HttpServletRequest request,AAdmins admins){
		ConstatFinalUtil.SYS_LOGGER.info("--adminsInsertSubmit---");
		admins.setPassword(this.encryptUtil.encodeStr(admins.getPassword()));
		admins.setCreateTime(new Date());
		admins.setUpdateTime(new Date());
		admins.setLastLoginTime(new Date());
		JSONObject resDbJSON= usersService.saveOneAdminsService(admins);
		info=resDbJSON.get("info")+"";
		JSONObject resultJSON=printJuiJson(request,info);
		return resultJSON.toJSONString();
	}
	@RequestMapping("/adminsUpdate")
	public String adminsUpdate(String aid,Model model){
		ConstatFinalUtil.SYS_LOGGER.info("---adminsUpdate--");
		Map<String, Object> condMap=new HashMap<String,Object>();
		condMap.put("id", aid);
		AAdmins admins=usersService.findOneAdminsService(condMap);
		model.addAttribute("admins",admins);
		condMap.clear();
		condMap.put("status", 1);
		List<ARole> roleList=usersService.findCondListRoleService(null, condMap);
		model.addAttribute("roleList", roleList);
		
		return "/back/admins/adminsUpdate";
	}
	@RequestMapping(value = "/adminsUpdateSubmit",produces="text/html;charset=UTF-8")
	@ResponseBody
	public String adminsUpdateSubmit(HttpServletRequest request,AAdmins admins)
	{
		ConstatFinalUtil.SYS_LOGGER.info("--adminsUpdateSubmit--");
		/* 根据id从数据库中再查询一次 
		 * 一一的赋值*/
		Map<String, Object> condMap = new HashMap<String, Object>();
		/* 根据Id查询管理员 */
		condMap.put("id", admins.getId());
		AAdmins adminsRes = this.usersService.findOneAdminsService(condMap);
		
		adminsRes.setEmail(admins.getEmail());
		adminsRes.setName(admins.getName());
		adminsRes.setPhone(admins.getPhone());
		adminsRes.setQq(admins.getQq());
		adminsRes.setSex(admins.getSex());
		adminsRes.setStatus(admins.getStatus());
		
		adminsRes.setUpdateTime(new Date());
		
		/* 千万千万别使用Spring的对象驱动;在Controller方法中增加一个对象 */
		JSONObject resDbJSON = this.usersService.updateOneAdminsService(adminsRes);
		this.info = resDbJSON.getString("info");
		JSONObject resultJSON = this.printJuiJson(request, info);
		return resultJSON.toJSONString(); 
	}
	@RequestMapping("/menuTreeList")
	public String menuTreeList(Model model)
	{
		ConstatFinalUtil.SYS_LOGGER.info("--menuTreeList--");
		/* 生成树形结构的字符串 */
		String result = this.authService.proccedMenuTreeStr("menu","");
		/* 存储字符串 */
		model.addAttribute("result", result);
		return "/back/admins/menuTreeList" ; 
	}
}
